/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using IQIGame.Onigao.Framework;
using System.Collections.Generic;
using System.Linq;
using System;
using NetProtocol.Enum;

namespace NetProtocol.POD {

	/// <summary>
	///  Game通知逻辑服准备玩家登录
	/// module : structs_internal
	/// </summary>
	public class LogicPlayerPOD : IPOD 
    {
        public readonly static Func<LogicPlayerPOD> FACTORY = () => new LogicPlayerPOD();
		public static Func<LogicPlayerPOD> DynamicFactory = FACTORY;
        
		/*-- members --*/
		/// <summary>
        /// 连接token
        /// </summary>
		public string token;
		/// <summary>
        /// 游戏服id	
        /// </summary>
		public string gameServerId;
		/// <summary>
        ///  连接逻辑服的原因，见gameServer.si
        /// </summary>
		public ConnectLogicReason reason;
		/// <summary>
        /// 
        /// </summary>
		public long pid;
		/// <summary>
        /// 
        /// </summary>
		public string name;
		/// <summary>
        /// 
        /// </summary>
		public int level;
		/// <summary>
        /// 
        /// </summary>
		public int exp;
		/// <summary>
        /// 
        /// </summary>
		public int avatar;
		/// <summary>
        /// 
        /// </summary>
		public int avatarFrame;
		/// <summary>
        /// 
        /// </summary>
        private Dictionary<int, int> m_playerParams;
		/// <summary>
        /// 基础道具
        /// </summary>
        private Dictionary<int, int> m_basicItems;
		/// <summary>
        /// 实例道具
        /// </summary>
        private List<ItemPOD> m_items;
		/// <summary>
        /// 道具冷却信息 cid => lastUsedTime
        /// </summary>
        private Dictionary<int, int> m_itemCdInfo;
		/// <summary>
        /// 道具的欠债
        /// </summary>
        private Dictionary<int, int> m_itemDebts;
		/// <summary>
        /// 自身英雄信息(全部)
        /// </summary>
        private List<HeroPOD> m_heroes;
		/// <summary>
        /// 编队信息
        /// </summary>
		public FormationPOD formation;
		/// <summary>
        /// 进入关卡数据
        /// </summary>
		public GameEnterLevelPOD enterLevelPOD;
		/// <summary>
        /// 进入战斗数据
        /// </summary>
		public GameEnterBattlePOD enterBattlePOD;
		/// <summary>
        /// 当前进行中的任务
        /// </summary>
        private List<QuestPOD> m_currentQuests;
		/// <summary>
        /// 已提交任务数据
        /// </summary>
        private Dictionary<int, int> m_committedQuests;
		/// <summary>
        /// 已失败任务数据
        /// </summary>
        private List<int> m_failedQuests;
		/// <summary>
        ///  快捷栏道具
        /// </summary>
		public long quickBarItemId;
		/// <summary>
        ///  正在追踪的任务
        /// </summary>
		public int trackQuestCid;
		/// <summary>
        /// 宠物数据	
        /// </summary>
        private List<PetPOD> m_pets;
		/// <summary>
        /// 当前主城客户端时间
        /// </summary>
		public long clientTime;
		
		
        public Dictionary<int, int> playerParams
        {
            get
            {
                if(m_playerParams == null)
                {
                    m_playerParams = new();
                }
                return m_playerParams;
            }
            private set => m_playerParams = value;
        }
        public Dictionary<int, int> unsafe_playerParams
        {
        	get => m_playerParams;
        	set => m_playerParams = value;
        }
        public Dictionary<int, int> basicItems
        {
            get
            {
                if(m_basicItems == null)
                {
                    m_basicItems = new();
                }
                return m_basicItems;
            }
            private set => m_basicItems = value;
        }
        public Dictionary<int, int> unsafe_basicItems
        {
        	get => m_basicItems;
        	set => m_basicItems = value;
        }
        public List<ItemPOD> items
        {
            get
            {
                if(m_items == null)
                {
                    m_items = new();
                }
                return m_items;
            }
            private set => m_items = value;
        }
        public List<ItemPOD> unsafe_items
        {
        	get => m_items;
        	set => m_items = value;
        }
        public Dictionary<int, int> itemCdInfo
        {
            get
            {
                if(m_itemCdInfo == null)
                {
                    m_itemCdInfo = new();
                }
                return m_itemCdInfo;
            }
            private set => m_itemCdInfo = value;
        }
        public Dictionary<int, int> unsafe_itemCdInfo
        {
        	get => m_itemCdInfo;
        	set => m_itemCdInfo = value;
        }
        public Dictionary<int, int> itemDebts
        {
            get
            {
                if(m_itemDebts == null)
                {
                    m_itemDebts = new();
                }
                return m_itemDebts;
            }
            private set => m_itemDebts = value;
        }
        public Dictionary<int, int> unsafe_itemDebts
        {
        	get => m_itemDebts;
        	set => m_itemDebts = value;
        }
        public List<HeroPOD> heroes
        {
            get
            {
                if(m_heroes == null)
                {
                    m_heroes = new();
                }
                return m_heroes;
            }
            private set => m_heroes = value;
        }
        public List<HeroPOD> unsafe_heroes
        {
        	get => m_heroes;
        	set => m_heroes = value;
        }
        public List<QuestPOD> currentQuests
        {
            get
            {
                if(m_currentQuests == null)
                {
                    m_currentQuests = new();
                }
                return m_currentQuests;
            }
            private set => m_currentQuests = value;
        }
        public List<QuestPOD> unsafe_currentQuests
        {
        	get => m_currentQuests;
        	set => m_currentQuests = value;
        }
        public Dictionary<int, int> committedQuests
        {
            get
            {
                if(m_committedQuests == null)
                {
                    m_committedQuests = new();
                }
                return m_committedQuests;
            }
            private set => m_committedQuests = value;
        }
        public Dictionary<int, int> unsafe_committedQuests
        {
        	get => m_committedQuests;
        	set => m_committedQuests = value;
        }
        public List<int> failedQuests
        {
            get
            {
                if(m_failedQuests == null)
                {
                    m_failedQuests = new();
                }
                return m_failedQuests;
            }
            private set => m_failedQuests = value;
        }
        public List<int> unsafe_failedQuests
        {
        	get => m_failedQuests;
        	set => m_failedQuests = value;
        }
        public List<PetPOD> pets
        {
            get
            {
                if(m_pets == null)
                {
                    m_pets = new();
                }
                return m_pets;
            }
            private set => m_pets = value;
        }
        public List<PetPOD> unsafe_pets
        {
        	get => m_pets;
        	set => m_pets = value;
        }
		
		/*-- constructors --*/
		public LogicPlayerPOD() {}
		public LogicPlayerPOD(string token, string gameServerId, ConnectLogicReason reason, long pid, string name, int level, int exp, int avatar, int avatarFrame, Dictionary<int, int> playerParams, Dictionary<int, int> basicItems, List<ItemPOD> items, Dictionary<int, int> itemCdInfo, Dictionary<int, int> itemDebts, List<HeroPOD> heroes, FormationPOD formation, GameEnterLevelPOD enterLevelPOD, GameEnterBattlePOD enterBattlePOD, List<QuestPOD> currentQuests, Dictionary<int, int> committedQuests, List<int> failedQuests, long quickBarItemId, int trackQuestCid, List<PetPOD> pets, long clientTime) {
			this.token = token;
			this.gameServerId = gameServerId;
			this.reason = reason;
			this.pid = pid;
			this.name = name;
			this.level = level;
			this.exp = exp;
			this.avatar = avatar;
			this.avatarFrame = avatarFrame;
			this.m_playerParams = playerParams;
			this.m_basicItems = basicItems;
			this.m_items = items;
			this.m_itemCdInfo = itemCdInfo;
			this.m_itemDebts = itemDebts;
			this.m_heroes = heroes;
			this.formation = formation;
			this.enterLevelPOD = enterLevelPOD;
			this.enterBattlePOD = enterBattlePOD;
			this.m_currentQuests = currentQuests;
			this.m_committedQuests = committedQuests;
			this.m_failedQuests = failedQuests;
			this.quickBarItemId = quickBarItemId;
			this.trackQuestCid = trackQuestCid;
			this.m_pets = pets;
			this.clientTime = clientTime;
		}
		  public void Serial(SOutputStream _out) {
                if(token != default) {
                    _out.WriteByte(1);
                    _out.Write_string(token);
                }
                if(gameServerId != default) {
                    _out.WriteByte(2);
                    _out.Write_string(gameServerId);
                }
                if(reason != default) {
                    _out.WriteByte(3);
                    _out.Write_byte((sbyte)reason);
                }
                if(pid != default) {
                    _out.WriteByte(4);
                    _out.Write_long(pid);
                }
                if(name != default) {
                    _out.WriteByte(5);
                    _out.Write_string(name);
                }
                if(level != default) {
                    _out.WriteByte(6);
                    _out.Write_int(level);
                }
                if(exp != default) {
                    _out.WriteByte(7);
                    _out.Write_int(exp);
                }
                if(avatar != default) {
                    _out.WriteByte(8);
                    _out.Write_int(avatar);
                }
                if(avatarFrame != default) {
                    _out.WriteByte(9);
                    _out.Write_int(avatarFrame);
                }
                if(m_playerParams != default) {
                    _out.WriteByte(10);
                    _out.Write_map_int_int(m_playerParams);
                }
                if(m_basicItems != default) {
                    _out.WriteByte(11);
                    _out.Write_map_int_int(m_basicItems);
                }
                if(m_items != default) {
                    _out.WriteByte(12);
                    _out.Write_list_pod(m_items);
                }
                if(m_itemCdInfo != default) {
                    _out.WriteByte(13);
                    _out.Write_map_int_int(m_itemCdInfo);
                }
                if(m_itemDebts != default) {
                    _out.WriteByte(14);
                    _out.Write_map_int_int(m_itemDebts);
                }
                if(m_heroes != default) {
                    _out.WriteByte(15);
                    _out.Write_list_pod(m_heroes);
                }
                if(formation != default) {
                    _out.WriteByte(16);
                    _out.Write_pod(formation);
                }
                if(enterLevelPOD != default) {
                    _out.WriteByte(17);
                    _out.Write_pod(enterLevelPOD);
                }
                if(enterBattlePOD != default) {
                    _out.WriteByte(18);
                    _out.Write_pod(enterBattlePOD);
                }
                if(m_currentQuests != default) {
                    _out.WriteByte(19);
                    _out.Write_list_pod(m_currentQuests);
                }
                if(m_committedQuests != default) {
                    _out.WriteByte(20);
                    _out.Write_map_int_int(m_committedQuests);
                }
                if(m_failedQuests != default) {
                    _out.WriteByte(21);
                    _out.Write_list_int(m_failedQuests);
                } 
                if(quickBarItemId != default) {
                    _out.WriteByte(22);
                    _out.Write_long(quickBarItemId);
                }
                if(trackQuestCid != default) {
                    _out.WriteByte(23);
                    _out.Write_int(trackQuestCid);
                }
                if(m_pets != default) {
                    _out.WriteByte(24);
                    _out.Write_list_pod(m_pets);
                }
                if(clientTime != default) {
                    _out.WriteByte(26);
                    _out.Write_long(clientTime);
                }
        }
            
        public void UnSerial(SInputStream _in) {
            while (_in.HasRemaining())
            {
                int _key = _in.ReadByte();
                switch (_key)
                {
                    case 1:
                        token = _in.Read_string();
                        break;
                    case 2:
                        gameServerId = _in.Read_string();
                        break;
                    case 3:
                        reason = (ConnectLogicReason)_in.Read_byte();
                        break;
                    case 4:
                        pid = _in.Read_long();
                        break;
                    case 5:
                        name = _in.Read_string();
                        break;
                    case 6:
                        level = _in.Read_int();
                        break;
                    case 7:
                        exp = _in.Read_int();
                        break;
                    case 8:
                        avatar = _in.Read_int();
                        break;
                    case 9:
                        avatarFrame = _in.Read_int();
                        break;
                    case 10:
                        m_playerParams = _in.Read_map_int_int();
                        break;
                    case 11:
                        m_basicItems = _in.Read_map_int_int();
                        break;
                    case 12:
                        m_items = _in.Read_list_pod<ItemPOD>(ItemPOD.DynamicFactory);
                        break;
                    case 13:
                        m_itemCdInfo = _in.Read_map_int_int();
                        break;
                    case 14:
                        m_itemDebts = _in.Read_map_int_int();
                        break;
                    case 15:
                        m_heroes = _in.Read_list_pod<HeroPOD>(HeroPOD.DynamicFactory);
                        break;
                    case 16:
                        formation = _in.Read_pod<FormationPOD>(FormationPOD.DynamicFactory);
                        break;
                    case 17:
                        enterLevelPOD = _in.Read_pod<GameEnterLevelPOD>(GameEnterLevelPOD.DynamicFactory);
                        break;
                    case 18:
                        enterBattlePOD = _in.Read_pod<GameEnterBattlePOD>(GameEnterBattlePOD.DynamicFactory);
                        break;
                    case 19:
                        m_currentQuests = _in.Read_list_pod<QuestPOD>(QuestPOD.DynamicFactory);
                        break;
                    case 20:
                        m_committedQuests = _in.Read_map_int_int();
                        break;
                    case 21:
                        m_failedQuests = _in.Read_list_int();
                        break;
                    case 22:
                        quickBarItemId = _in.Read_long();
                        break;
                    case 23:
                        trackQuestCid = _in.Read_int();
                        break;
                    case 24:
                        m_pets = _in.Read_list_pod<PetPOD>(PetPOD.DynamicFactory);
                        break;
                    case 26:
                        clientTime = _in.Read_long();
                        break;
                    default: _in.SkipBinary();break;
                }
            }
        }
		
		public LogicPlayerPOD Clone() {
            LogicPlayerPOD o = DynamicFactory();
            o.token = token;
            o.gameServerId = gameServerId;
            o.reason = reason;
            o.pid = pid;
            o.name = name;
            o.level = level;
            o.exp = exp;
            o.avatar = avatar;
            o.avatarFrame = avatarFrame;
            if (m_playerParams != null) {
                o.m_playerParams = new (m_playerParams);
            }
            if (m_basicItems != null) {
                o.m_basicItems = new (m_basicItems);
            }
            if (m_items != null) {
                o.m_items = m_items.ConvertAll(o => o?.Clone());
            }
            if (m_itemCdInfo != null) {
                o.m_itemCdInfo = new (m_itemCdInfo);
            }
            if (m_itemDebts != null) {
                o.m_itemDebts = new (m_itemDebts);
            }
            if (m_heroes != null) {
                o.m_heroes = m_heroes.ConvertAll(o => o?.Clone());
            }
            if (formation != null) {
                o.formation = formation.Clone();
            }
            if (enterLevelPOD != null) {
                o.enterLevelPOD = enterLevelPOD.Clone();
            }
            if (enterBattlePOD != null) {
                o.enterBattlePOD = enterBattlePOD.Clone();
            }
            if (m_currentQuests != null) {
                o.m_currentQuests = m_currentQuests.ConvertAll(o => o?.Clone());
            }
            if (m_committedQuests != null) {
                o.m_committedQuests = new (m_committedQuests);
            }
            if (m_failedQuests != null) {
                o.m_failedQuests = new (m_failedQuests);
            }
            o.quickBarItemId = quickBarItemId;
            o.trackQuestCid = trackQuestCid;
            if (m_pets != null) {
                o.m_pets = m_pets.ConvertAll(o => o?.Clone());
            }
            o.clientTime = clientTime;
            return o;
        }
        
        public void Clear() {
            token = default;
            gameServerId = default;
            reason = default;
            pid = default;
            name = default;
            level = default;
            exp = default;
            avatar = default;
            avatarFrame = default;
            m_playerParams = default;
            m_basicItems = default;
            m_items = default;
            m_itemCdInfo = default;
            m_itemDebts = default;
            m_heroes = default;
            formation = default;
            enterLevelPOD = default;
            enterBattlePOD = default;
            m_currentQuests = default;
            m_committedQuests = default;
            m_failedQuests = default;
            quickBarItemId = default;
            trackQuestCid = default;
            m_pets = default;
            clientTime = default;
        }
		
        public override string ToString() {
			string str = "";
			str += "LogicPlayerPOD [";
			str += "token=";
			str += token;
			str += ", gameServerId=";
			str += gameServerId;
			str += ", reason=";
			str += reason;
			str += ", pid=";
			str += pid;
			str += ", name=";
			str += name;
			str += ", level=";
			str += level;
			str += ", exp=";
			str += exp;
			str += ", avatar=";
			str += avatar;
			str += ", avatarFrame=";
			str += avatarFrame;
			str += ", playerParams=";
			str += m_playerParams.ToString<int, int>();
			str += ", basicItems=";
			str += m_basicItems.ToString<int, int>();
			str += ", items=";
			str += m_items.ToString<ItemPOD>();
			str += ", itemCdInfo=";
			str += m_itemCdInfo.ToString<int, int>();
			str += ", itemDebts=";
			str += m_itemDebts.ToString<int, int>();
			str += ", heroes=";
			str += m_heroes.ToString<HeroPOD>();
			str += ", formation=";
			str += formation;
			str += ", enterLevelPOD=";
			str += enterLevelPOD;
			str += ", enterBattlePOD=";
			str += enterBattlePOD;
			str += ", currentQuests=";
			str += m_currentQuests.ToString<QuestPOD>();
			str += ", committedQuests=";
			str += m_committedQuests.ToString<int, int>();
			str += ", failedQuests=";
			str += m_failedQuests.ToString<int>();
			str += ", quickBarItemId=";
			str += quickBarItemId;
			str += ", trackQuestCid=";
			str += trackQuestCid;
			str += ", pets=";
			str += m_pets.ToString<PetPOD>();
			str += ", clientTime=";
			str += clientTime;
			str += "]";
			return str;
		}
		
	}
}